
using UnityEngine.EventSystems;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

public class color : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
    public RectTransform yaogan_shangbu, yaogan_dibu;
    public Vector2 yaoganshangbu_chushiweizhi, yaogandibuchushiweizhi, yaoganhuadongfangxiang, yaoganhuadongfangxiang_guiyihua, yidongfangxiang;
    public float huadong_changdu, huadong_fan_wei, player_move_speed;
    public Transform player;
    public Vector2 player_move_dire;
   
   


    public void Start()
    {
      
    }


    public void OnDrag(PointerEventData eventData)
    {
        
        yaoganhuadongfangxiang = eventData.position - yaoganshangbu_chushiweizhi ;
        huadong_changdu = yaoganhuadongfangxiang.magnitude;
        huadong_fan_wei = Mathf.Clamp(huadong_changdu, 0, yaogan_dibu.rect.width * 2);
        yaoganhuadongfangxiang_guiyihua = yaoganhuadongfangxiang.normalized;
        yidongfangxiang = yaoganshangbu_chushiweizhi + yaoganhuadongfangxiang_guiyihua * huadong_fan_wei;
        yaogan_shangbu.position = yidongfangxiang;
        player_move_dire = new Vector2(yaoganhuadongfangxiang_guiyihua.x, yaoganhuadongfangxiang_guiyihua.y);



    }



    public void Update()
    {
        yaoganshangbu_chushiweizhi = yaogan_shangbu.position;
        yaogandibuchushiweizhi = yaogan_dibu.position;
        player.Translate(player_move_dire * player_move_speed * Time.deltaTime);
       
      
    }


    public void OnPointerDown(PointerEventData eventData)
    {


        yaogan_shangbu.position=eventData.position;
        yaogan_dibu.position = eventData.position;
        player_move_speed = 500;
        yaogan_dibu.gameObject.SetActive(true);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        
        
        //yaoganhuadongfangxiang_guiyihua = Vector2.zero;
        player_move_speed = 0;
        yaogan_dibu.gameObject.SetActive(false);
    }

}